﻿using System;
using System.Collections.Generic;
using System.Linq;
using Fee.Stores;
using Identity;
using Identity.Stores;
using Identity.ViewModels;

namespace JwtWebApi.Models.Manage
{
    public class UserViewModel
    {
        public UserViewModel()
        {
        }

        public UserViewModel(User user, IEnumerable<Role> rolesName = null)
        {
            if (user == null) throw new ArgumentNullException(nameof(user));

            UserName = user.UserName;
            Name = user.Name;
            Email = user.Email;
            NormalizedEmail = user.NormalizedEmail;
            NormalizedUserName = user.NormalizedUserName;
            Id = user.Id;
            PhoneNumber = user.PhoneNumber;
            LockoutEnabled = user.LockoutEnabled;
            if (user.Vendor != null)
                Vendor = new VendorSimpleViewModel(user.Vendor);
            if (rolesName != null)
                Roles = rolesName.Select(f => f.Name);
            District = user.District;
        }
        public District District { get; set; }
        public IEnumerable<string> Roles { get; set; } = new List<string>();
        public VendorSimpleViewModel Vendor { get; set; }
        public bool LockoutEnabled { get; set; }

        public string PhoneNumber { get; set; }

        public string Id { get; set; }

        public string Email { get; set; }
        public string NormalizedEmail { get; set; }
        public string UserName { get; set; }
        public string NormalizedUserName { get; set; }
        public string Name { get; set; }

        public void FillTo<T>(User user, IVendorStore<T> store, IDistrictStore<T> districtStore) where T : MemberDbContextBase
        {
            if (String.IsNullOrEmpty(Id))
            {
                user.UserName = this.UserName;
            }
            if (!string.IsNullOrEmpty(this.UserName) && user.UserName != this.UserName)
                user.UserName = this.UserName;
            user.Name = Name;
            user.Email = Email;
            user.PhoneNumber = PhoneNumber;
            if (District != null)
                user.District = districtStore.GetById(this.District.Id);
            if (Vendor != null)
                user.Vendor = store.GetById(Vendor.Id);
        }
    }
}